local niepan = fk.CreateSkill {

  name = "joyex__niepan",

  tags = { Skill.Limited, },

}



niepan:addEffect(fk.AskForPeaches, {
  name = "joyex__niepan",
  anim_type = "defensive",
  frequency = Skill.Limited,
  events = {fk.AskForPeaches},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(niepan.name) and player.dying and player:usedSkillTimes(niepan.name, Player.HistoryGame) == 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if player.general == "joyex__pangtong" then 
      player.general = "joy_skin__pangtong"
      room:broadcastProperty(player, "general")
      room:notifySkillInvoked(player, "joyex__niepan","drawcard")
    elseif player.deputyGeneral == "joyex__pangtong" then
      player.deputyGeneral = "joy_skin__pangtong"
      room:broadcastProperty(player, "deputyGeneral")
      room:notifySkillInvoked(player, "joyex__niepan","drawcard")
    end
    player:throwAllCards("hej")
    if player.dead then return false end
    player:reset()
    if player.dead then return false end
    player:drawCards(3, niepan.name)
    if player.dead then return false end
    if player:isWounded() then
      room:recover({
        who = player,
        num = math.min(3, player.maxHp) - player.hp,
        recoverBy = player,
        skillName = niepan.name,
      })
      if player.dead then return false end
    end
    local choices = {}
    if not player:hasSkill("joy__huoji",true) then
      table.insert(choices,"joy__huoji")
    end
    if not player:hasSkill("joy__bazhen") and not player:hasSkill("joy__kanpo") then
      table.insert(choices,"joy__bazhen|joy__kanpo")
    end
    if #choices == 0 then return false end
    local choice = player.room:askForChoice(player, choices, niepan.name, "#joyex__niepan-choice", true,{"joy__huoji","joy__bazhen|joy__kanpo"})
    room:handleAddLoseSkills(player, choice, nil)
  end,
})

return niepan